// pages/activitys/join/join.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId: '',
    isTip: '',
    gender: -1,
    genders:['保密', '女', '男'],
    successBgImg: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      activityId: options.id
    });
    console.log('onLoad');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log('onReady');
    this.loadSuccessImg ();
  },

  loadSuccessImg () {
    let _that = this;
    var API_KEY = '16726550-3dabe0cd680611df2f42b220a';
    let _categorys = ['nature','travel'];
    var URL = "https://pixabay.com/api/?key="+API_KEY+"&q=山&per_page=6&category="+_categorys[Math.ceil(Math.random()*1)]+"&min_width=1240&min_height=632&image_type=photo";
    console.log(URL);
    wx.request({
      url: URL,
      success(res) {
        let n = Math.ceil(Math.random()*5);
        let dd = res.data.hits[n];
        console.log(dd);
        console.log(dd.webformatURL);

        _that.setData({
          successBgImg: dd.webformatURL
        });
      }
    });
  },

  changeGender(e) {
    this.setData({
      gender: Number(e.detail.value)
    })
  },
  formSubmit(e) {
    let _that = this;
    let _fd = e.detail.value;
    
    if(_fd.name){
      wx.showToast({
        title: '请输入姓名',
        icon: 'warn',
        duration: 2000
      });
      return false;
    }else if(_fd.tel){
      wx.showToast({
        title: '请输入联系电话',
        icon: 'warn',
        duration: 2000
      });
      return false;
    }
    _that.setData({
      isTip: 'Modal'
    });
    return false;
    let _oId = wx.getStorageSync('loginUserId');
    //console.log('form发生了submit事件，携带数据为：', e.detail.value)
    let _dd = {
      "userId": "",
      "activityId": this.data.activityId,
      "userName": _fd.name,
      "gender": this.data.gender,
      "idCard": _fd.idCard,
      "tel": _fd.tel,
      "openId": _oId
    };
    
    let _url = app.globalData.apiServerUrl + '/join';
    wx.request({
      url: _url,
      method: 'POST',
      data: _dd,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success (res) {
        console.log(res);
        let _rd = res.data;
        if(_rd.code = 200 && _rd.status){
          _that.setData({
            isTip: 'Modal'
          });
        }
      }
    })
  },

  hideModal(e) {
    this.setData({
      isTip: ''
    });
    wx.redirectTo({
      url: '/pages/index/index'
    });
  }

})